package animationCollision;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DrawPanel extends JPanel implements Runnable {
	List<Shape> shapes = new ArrayList<Shape>();
	boolean isThreadStarted;

	public DrawPanel(Rectangle bounds) {
		setBounds(bounds);
		initShapes();
		isThreadStarted = true;
		new Thread(this).start();
	}

	private void initShapes() {
		shapes.add(new Ball(200, 20, 200, 50, 30,Color.yellow));
		shapes.add(new Ball(100, 40));
		shapes.add(new Ball(50, 500, 200, 50, 30,Color.red));
	}

	@Override
	public void run() {
		while (isThreadStarted) {
			for (Shape shape : shapes) {
//				Random r = new Random(10);
//				if (shape instanceof Ball) {
//					Ball b = (Ball) shape;
//					b.radius = r.nextFloat();
//				}
				shape.speedX=0;
				repaint();
			}
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		System.out.println("painting...");
		super.paintComponent(g);
		if (isThreadStarted) {
			for (Shape shape : shapes) {
				shape.draw(g);
				System.out.println(shape);
			}
			
		}
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame("Shapes");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				// BallWorld is a JPanel
				Rectangle rect = new Rectangle(0, 0, 640, 480);
				frame.setContentPane(new DrawPanel(rect));
				frame.pack(); // Preferred size of BallWorld
				frame.setBounds(rect);
				frame.setVisible(true); // Show it
			}
		});

	}

}
